<template>
	<view class="searchBox">
		<view class="inputOut">
			<view class="innerBox">
				<u-search placeholder="关键词搜索" v-model="searchValue" :clearabled="true" :showAction="true"
					@search="handleSearch" @custom="handleSearch" @clear="handleclear"></u-search>
			</view>
		</view>
		<!-- <view class="" style="height: 100rpx;"></view> -->
		<view v-if="productList.length==0" class="historyBox">
			<view class="history_top">
				<view class="">
					历史搜索
				</view>
				<image src="../../static/image/del.png" style="width: 40rpx;height: 40rpx;" mode=""
					@click="delshow = true"></image>
			</view>
			<view class="history_bottom">
				<view class="history_item" v-for="(item,index) in historyArr" :key="index"
					@click="handleHistorysearch(item)">
					{{item}}
				</view>
			</view>
		</view>

		<view v-if="productList.length>0" class="tabsBox">
			<view class="tabsItem" :class="currIndex == true ?'tabsItemed' :''" @click="handleTj">
				推荐
			</view>
			<view class="tabsItem" :class="volumeIndex != null ?'tabsItemed' :''" @click="handleVolume">
				<view class="">
					销量
				</view>
				<image v-if="volumeIndex==null" src="../../static/image/bot.png" style="width: 28rpx;height: 28rpx;"
					mode=""></image>
				<image v-if="volumeIndex==true" src="../../static/image/up.png" style="width: 28rpx;height: 28rpx;"
					mode=""></image>
				<image v-if="volumeIndex==false" src="../../static/image/down1.png" style="width: 28rpx;height: 28rpx;"
					mode=""></image>
			</view>
			<view class="tabsItem" :class="priceIndex != null ?'tabsItemed' :''" @click="handlePrice">
				<view class="">
					价格
				</view>
				<image v-if="priceIndex==null" src="../../static/image/bot.png" style="width: 28rpx;height: 28rpx;"
					mode=""></image>
				<image v-if="priceIndex==true" src="../../static/image/up.png" style="width: 28rpx;height: 28rpx;"
					mode=""></image>
				<image v-if="priceIndex==false" src="../../static/image/down1.png" style="width: 28rpx;height: 28rpx;"
					mode=""></image>
			</view>
		</view>


		<view v-if="productList.length>0" class="dataList">
			<view class="dataItem" v-for="(item,index) in productList" :key="index" @click="handleDetail(item)">
				<image :src="item.image" style="width: 336rpx;height: 336rpx;border-radius: 20rpx;" mode=""></image>
				<view class="" style="padding: 0 10rpx; box-sizing: border-box;">
					<view class="name">
						{{item.name}}
					</view>
					<!-- <view class="desc">
						补充维C | 低卡轻蔬
					</view> -->
					<view class="priceBox">
						<view class="price">
							₱{{item.price}}
						</view>
						<view class="rightprice">
							/份 <text v-if="item.market_price">₱{{item.market_price}}</text>
						</view>
					</view>

					<view class="bottomBox">
						<view class="ys">
							已售{{item.sold_count}}
						</view>
						<image src="../../static/image/tocar.png" style="width: 48rpx;height: 48rpx;" mode="">
						</image>
					</view>
				</view>

			</view>
		</view>

		<view class="" style="height: 100rpx;"></view>

		<u-modal :show="delshow" :title="title" :showCancelButton="true" @confirm="handleDelhitory"
			@cancel="delshow = false" confirmColor="#439B05"></u-modal>

	</view>
</template>

<script>
	import {
		productslist
	} from "@/api/api.js"
	export default {
		data() {
			return {
				currIndex: true,
				volumeIndex: null,
				priceIndex: null,
				searchValue: "",
				page: 1,
				page_size: 10,
				productList: [],
				is_price: null, //价格
				sold_count: null, //销量
				historyArr: [],
				title: "确定要删除历史搜索吗？",
				delshow: false,
			}
		},
		onLoad() {
			if (uni.getStorageSync('historyData')) {
				this.historyArr = uni.getStorageSync('historyData')
			}
		},
		onReachBottom() {
			this.page++;
			this.handleSearch();
		},
		methods: {
			async handleSearch() {
				let res = await productslist({
					page: this.page,
					page_size: this.page_size,
					name: this.searchValue,
					is_price: this.is_price,
					sold_count: this.sold_count
				})

				res.data.product.data.forEach((item, index) => {
					this.productList.push(item)
				})
				//历史搜索
				if (this.searchValue == '') return
				if (uni.getStorageSync('historyData')) {
					let caData = uni.getStorageSync('historyData')
					caData.unshift(this.searchValue)
					//去重
					const uniqueArr = Array.from(new Set(caData))
					uni.setStorageSync('historyData', uniqueArr)
					this.historyArr = uni.getStorageSync('historyData')
				} else {
					uni.setStorageSync('historyData', [this.searchValue])
					this.historyArr = uni.getStorageSync('historyData')
				}
				// console.log("ssss", this.historyArr)

			},
			handleHistorysearch(item) {
				this.searchValue = item
				this.page = 1
				this.handleSearch()
			},
			handleclear() {
				this.page = 1
				this.productList = []
			},
			handleTj() {
				this.currIndex = !this.currIndex
				this.volumeIndex = null
				this.priceIndex = null
			},
			handleVolume() {
				this.currIndex = false
				if (this.volumeIndex == null) {
					this.volumeIndex = true
					return
				}
				this.volumeIndex = !this.volumeIndex
				if (this.volumeIndex) {
					this.sold_count = 1
				} else {
					this.sold_count = 2
				}
				this.handleSearch()
			},
			handlePrice() {
				this.currIndex = false
				if (this.priceIndex == null) {
					this.priceIndex = true
					return
				}
				this.priceIndex = !this.priceIndex
				if (this.priceIndex) {
					this.is_price = 1
				} else {
					this.is_price = 2
				}
				this.handleSearch()
			},

			handleDetail(item) {
				console.log("33", item)
				uni.navigateTo({
					url: `./detail?id=${item.id}`
				})
			},
			handleDelhitory() {
				uni.removeStorageSync('historyData')
				this.historyArr = []
				this.delshow = false
			},

		}
	}
</script>

<style scoped lang="scss">
	.searchBox {
		width: 100%;
		min-height: 100%;
		background-color: #ffffff;

		.inputOut {
			width: 100%;
			padding: 10rpx 30rpx;
			box-sizing: border-box;
			// position: fixed;
			// top: 100rpx;
			// background-color: #ffffff;

			.innerBox {
				width: 690rpx;
				height: 80rpx;
				background: #F5F5F5;
				border-radius: 40rpx 40rpx 40rpx 40rpx;
				padding: 20rpx 30rpx;
				box-sizing: border-box;
				display: flex;
			}

		}

		.historyBox {
			width: 100%;
			margin-top: 30rpx;
			padding: 0 30rpx;
			box-sizing: border-box;

			.history_top {
				width: 100%;
				font-size: 30rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333333;
				line-height: 35rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.history_bottom {
				display: flex;
				flex-wrap: wrap;
				margin-top: 30rpx;

				.history_item {
					// width: 96rpx;
					height: 48rpx;
					padding: 0 20rpx;
					background-color: #F5F5F5;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #666666;
					line-height: 48rpx;
					text-align: center;
					border-radius: 100rpx;
					margin-right: 12rpx;
					margin-bottom: 30rpx;
				}
			}
		}

		.tabsBox {
			width: 750rpx;
			height: 88rpx;
			background: #FFFFFF;
			padding: 22rpx 30rpx;
			box-sizing: border-box;
			display: flex;

			.tabsItem {
				display: flex;
				align-items: center;
				margin-right: 100rpx;
				font-size: 28rpx;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 400;
				color: #999999;
				line-height: 33rpx;
			}

			.tabsItemed {
				color: #439B05;
			}

		}

		.dataList {
			width: 100%;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.dataItem {
				width: 336rpx;
				// height: 564rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(0, 0, 0, 0.08);
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				margin-bottom: 20rpx;
				padding-bottom: 20rpx;

				.name {
					width: 316rpx;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #333333;
					margin-bottom: 10rpx;
					line-height: 33rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.desc {
					font-size: 20rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #439B05;
				}

				.priceBox {
					display: flex;
					margin-top: 10rpx;

					.price {
						font-size: 28rpx;
						font-family: DIN, DIN;
						font-weight: 500;
						color: #FF3D3D;
						line-height: 33rpx;
					}

					.rightprice {
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: #BBBBBB;
						line-height: 28rpx;
					}
				}

				.bottomBox {
					display: flex;
					width: 100%;
					justify-content: space-between;
					font-size: 20rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #666666;
					line-height: 23rpx;
					align-items: center;
				}
			}
		}
	}
</style>